/**
 * 全局原生 js 文件
 * */
(function(window){

    var C = {};

    /**
     * 倒计时
     * @desc 调用该方法，传入对象参数，参数为倒计时的 天、时、分、秒，必须为整数
     * @param option(Object)
     * @param day : 天
     * @param hour : 时
     * @param minute : 分
     * @param second : 秒
     * @param milliSeconds : 毫秒
     * */
    function countdown(option) {
        var _dayParam = option['day'] || 0;
        var _hourParam = option['hour'] || 0;
        var _minuteParam = option['minute'] || 0;
        var _secondParam = option['second'] || 0;
        var _milliSecondsParam = option['milliSeconds'] || 0;
        var _ele = option['ele'];

        //判断传入的节点是否正确
        if (!_ele || !_ele.nodeType || _ele.nodeType != 1) {
            throw 'The parameter [ele] is not html element!';
        }

        var date = new Date();
        var _oldTime = date.getTime();
        var _newTime = date.getTime();

        var _totalSecondsParam = 0;
        if (_dayParam && !isNaN(_dayParam) && _dayParam > 0) {
            _totalSecondsParam += _dayParam * 24 * 60 * 60 * 1000
        }

        if (_hourParam && !isNaN(_hourParam) && _hourParam > 0) {
            _totalSecondsParam += _hourParam * 60 * 60 * 1000;
        }

        if (_minuteParam && !isNaN(_minuteParam) && _minuteParam > 0) {
            _totalSecondsParam += _minuteParam * 60 * 1000;
        }

        if (_secondParam && !isNaN(_secondParam) && _secondParam > 0) {
            _totalSecondsParam += _secondParam * 1000;
        }

        if (_milliSecondsParam && !isNaN(_milliSecondsParam) && _milliSecondsParam > 0) {
            _totalSecondsParam += _milliSecondsParam;
        }

        //倒计时时间设置没有差值
        if (_totalSecondsParam == 0) {
            return false;
        }
        _newTime = _oldTime - _totalSecondsParam;

        var _oldDate = new Date();
        var countDownInterval = window.setInterval(function () {
            var _nowDate = new Date();
            var _milliseconds = _nowDate - _oldDate;
            var _addTime = _newTime + _milliseconds;
            if (_addTime <= _oldTime) {
                var _cdDay = Math.floor((_oldTime - _addTime) / 1000 / 60 / 60 / 24);
                var _cdHour = Math.floor(((_oldTime - _addTime) / 1000 / 60 / 60) % 24);
                var _cdMinutes = Math.floor(((_oldTime - _addTime) / 1000 / 60) % 60);
                var _cdSecond = Math.floor(((_oldTime - _addTime) / 1000) % 60);
                var _cdMilliseconds = Math.floor((_oldTime - _addTime) % 1000);

                if (_cdDay >= 0) {
                    _cdDay.toString().length == 1 ? _cdDay = "0" + _cdDay : _cdDay = _cdDay;
                } else {
                    _cdDay = 0;
                }

                if (_cdHour >= 0) {
                    _cdHour.toString().length == 1 ? _cdHour = "0" + _cdHour : _cdHour = _cdHour;
                } else {
                    _cdHour = 0;
                }

                if (_cdMinutes >= 0) {
                    _cdMinutes.toString().length == 1 ? _cdMinutes = "0" + _cdMinutes : _cdMinutes = _cdMinutes;
                } else {
                    _cdMinutes = 0;
                }

                if (_cdSecond >= 0) {
                    _cdSecond.toString().length == 1 ? _cdSecond = "0" + _cdSecond : _cdSecond = _cdSecond;
                } else {
                    _cdSecond = 0;
                }

                if (_cdMilliseconds >= 0) {
                    _cdMilliseconds.toString().length == 1 ? _cdMilliseconds = "00" + _cdMilliseconds : _cdMilliseconds = _cdMilliseconds;
                    _cdMilliseconds.toString().length == 2 ? _cdMilliseconds = "0" + _cdMilliseconds : _cdMilliseconds = _cdMilliseconds;
                }

                //补0
                if (_ele && (_ele.textContent || _ele.textContent.trim() == "")) {
                    _ele.textContent = _cdDay + "  -  " + _cdHour + "-" + _cdMinutes + "-" + _cdSecond + "-" + _cdMilliseconds;
                }
            } else {
                //倒计时结束
                window.clearInterval(countDownInterval);
            }
        }, 1);
    }
    C.countdown = C['countdown'] = countdown;

    window.C = window['C'] = C;
})(window);